/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.odg.dg.runtimedata;

import java.util.AbstractMap;
import java.util.Set;
import org.apache.commons.lang.text.StrSubstitutor;

/**
 *
 * @author yura
 */
public class VariableResolver {

    /**
     * For example you have string with some keys: <br>
     * <pre>
     * {@code "Hello Mr ${name}, I want to congratulate you with ${celebration}."}
     * </pre>
     * And you want to replace keys in bracket on the real value. It will be
     * look like this:
     * <pre>
     * {@code
     * String template = "Hello Mr ${name}, I want to congratulate you with ${celebration}"
     * Properties values = new Properties();
     * values.put("name", "Broun");
     * values.put("celebration", "birthday");
     * String result = assignVarValue(template, new VariableHolder(null, new GlobalVariables(values)));
     * // String result will contain: "Hello Mr Broun, I want to congratulate you with birthday"
     * }
     * </pre>
     *
     * @param string template with the keys
     * @param values map that contain value for each key. If this map will not
     * contain some value you will get exception!
     * @return parsed string
     */
    public static String assignVarValue(String string, final VariableHolder values) {
//        return assignVarValue(0, string, values);
        return StrSubstitutor.replace(string, new AbstractMap() {

            @Override
            public Set entrySet() {
                throw new UnsupportedOperationException("Not supported.");
            }

            @Override
            public Object get(Object key) {
                return values.getVarValue(String.valueOf(key));
            }
        });
    }

//    /**
//     * @see #assignVarValue(String, VariableResolver.VariableHolder)
//     * @param startPoint position where from start parsing
//     */
//    public static String assignVarValue(int startPoint, String string,
//            VariableHolder values){
//
//        int start, end;
//
//        if ((start = string.indexOf("${", startPoint)) > 0
//                && (end = string.indexOf("}", start)) > 0){
//            String v;
//            String key = string.substring(start + 2, end );
//            string = string.substring(0, start)
//                    + (v = values.getVarValue(key))
//                    + string.substring(end + 1, string.length());
//
//            if (v == null){
//                throw new IllegalArgumentException(String.format("values [%s],"
//                        + " do not containe [%s]", values.toString(), key));
//            }
//
//            return assignVarValue(start + v.length(), string, values);
//        }
//
//        return string;
//    }
}
